﻿using System;
using System.Collections.Generic;
using System.Text;
using com.crawler.common;
using com.crawler.db.Interface;

namespace com.crawler.components
{
    public class DBManager
    {
        /// <summary>
        /// 系统配置
        /// </summary>
        private SysConfigInfo m_sys_config = null;

        public DBManager()
        {
            try
            {
                m_sys_config = SerializationHelper.Load(typeof(SysConfigInfo), Consts.SysConfigFileName) as SysConfigInfo;

            }
            catch (Exception ex)
            {
                LogHelper.Error("加载系统配置信息失败" + ex.ToString());
            }
        }

        /// <summary>
        /// 创建表结构
        /// </summary>
        /// <param name="i_table_name"></param>
        /// <returns></returns>
        public int CreateChapterTable(string i_table_name)
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(m_sys_config.DBType);

            try
            {
                return t_db_mgr.CreateTable(i_table_name, m_sys_config.DBLinkString);
            }
            catch (Exception ex)
            {
                LogHelper.Error("创建表结构失败" + ex.ToString());
                return -10;
            }
        }

        
        /// <summary>
        /// 插入站点信息
        /// </summary>
        /// <param name="i_site_id"></param>
        /// <param name="i_site_name"></param>
        /// <param name="i_tb_name"></param>
        /// <param name="i_connect_string"></param>
        /// <returns></returns>
        public int InsertBookSite(int i_site_id, string i_site_name, string i_tb_name,string i_site_enconding,string i_chapter_content_reg,string i_chapter_content_filter)
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(m_sys_config.DBType);

            try
            {
                return t_db_mgr.InsertBookSite(i_site_id, i_site_name, i_tb_name,i_site_enconding,i_chapter_content_reg,i_chapter_content_filter, m_sys_config.DBLinkString);
            }
            catch (Exception ex)
            {
                LogHelper.Error("插入站点信息失败" + ex.ToString());
                return -1;
            }
        }

        /// <summary>
        /// 初始化站点到数据库
        /// </summary>
        /// <param name="i_book_site_list"></param>
        /// <returns></returns>
        public int InitBookSite(List<BookSiteInfo> i_book_site_list)
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(m_sys_config.DBType);

            try
            {
                return t_db_mgr.InitBookSite(i_book_site_list,m_sys_config.DBLinkString);
            }
            catch (Exception ex)
            {
                LogHelper.Error("初始化数据库脚本" + ex.ToString());
                return -1;
            }
        }

        /// <summary>
        /// 初始化数据库脚本
        /// </summary>
        /// <param name="i_db_type"></param>
        /// <param name="i_connect_string"></param>
        /// <returns></returns>
        public int InitDBScript(Consts.DBType i_db_type,string i_connect_string)
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(i_db_type);

            try
            {
                return t_db_mgr.InitDBScript(i_connect_string);
            }
            catch (Exception ex)
            {
                LogHelper.Error("初始化数据库脚本" + ex.ToString());
                return -10;
            }
        }

        /// <summary>
        /// 添加小说信息
        /// </summary>
        /// <param name="i_book"></param>
        /// <returns></returns>
        public long AddBook(BookInfo i_book)
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(m_sys_config.DBType);

            try
            {
                return t_db_mgr.AddBook(i_book, m_sys_config.DBLinkString);
            }
            catch (Exception ex)
            {
                LogHelper.Error("初始化数据库脚本" + ex.ToString());
                return -1;
            }
        }

        /// <summary>
        /// 获取需要更新的小说队列
        /// </summary>
        /// <param name="i_site_list"></param>
        /// <param name="i_connect_string"></param>
        /// <returns></returns>
        public Queue<SiteUrl> GetUpdateBook(List<BookSiteInfo> i_site_list, int i_page_index)
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(m_sys_config.DBType);

            try
            {
                return t_db_mgr.GetUpdateBook(i_site_list,i_page_index,  m_sys_config.DBLinkString);
            }
            catch (Exception ex)
            {
                LogHelper.Error("初始化数据库脚本" + ex.ToString());
                return new  Queue<SiteUrl>() ;
            }
        }

         /// <summary>
        /// 获取需要更新的小说章节
        /// </summary>
        /// <returns></returns>
        public List<BookInfo> GetUpdateBookList()
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(m_sys_config.DBType);

            try
            {
                return t_db_mgr.GetUpdateBookList( m_sys_config.DBLinkString);
            }
            catch (Exception ex)
            {
                LogHelper.Error("初始化数据库脚本" + ex.ToString());
                return new List<BookInfo>();
            }
        }

         /// <summary>
        /// 批量插入章节信息
        /// </summary>
        /// <param name="i_book_id"></param>
        /// <param name="i_book_name"></param>
        /// <param name="i_chapter_list"></param>
        /// <param name="i_connect_string"></param>
        /// <param name="i_table_name"></param>
        /// <returns></returns>
        public int AddChapterList(long i_book_id, string i_book_name, List<ChapterInfo> i_chapter_list, string i_table_name, int i_site_id,string i_site_name)
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(m_sys_config.DBType);

            try
            {
                return t_db_mgr.AddChapterList(i_book_id, i_book_name, i_chapter_list, m_sys_config.DBLinkString, i_table_name, i_site_id, i_site_name);
            }
            catch (Exception ex)
            {
                LogHelper.Error("初始化数据库脚本" + ex.ToString());
                return  -1;
            }
        }

        public BookInfo GetBookInfoByName(string i_book_name)
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(m_sys_config.DBType);

            try
            {
                return t_db_mgr.GetBookInfoByName(i_book_name, m_sys_config.DBLinkString);
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取小说信息失败" + ex.ToString());
                return  null;
            }
        }

        /// <summary>
        /// 获取小说章节信息
        /// </summary>
        /// <param name="i_book_id"></param>
        /// <returns></returns>
        public List<ChapterInfo> GetChapterByBookId(long i_book_id)
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(m_sys_config.DBType);

            try
            {
                return t_db_mgr.GetChapterByBookId(i_book_id, m_sys_config.DBLinkString);
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取小说章节信息失败" + ex.ToString());
                return null;
            }
        }

         /// <summary>
        /// 更新小说下载状态
        /// </summary>
        /// <param name="i_chapter_id"></param>
        /// <param name="i_connect_string"></param>
        /// <returns></returns>
        public int UpdateChapter(long i_book_id, long i_chapter_id,string i_chapter_url,BookUrlInfo i_book_url)
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(m_sys_config.DBType);

            try
            {
                return t_db_mgr.UpdateChapter(i_book_id, i_chapter_id, i_chapter_url, i_book_url, m_sys_config.DBLinkString);
            }
            catch (Exception ex)
            {
                LogHelper.Error("更新小说下载状态失败" + ex.ToString());
                return -1;
            }
        }

         /// <summary>
        /// 添加热点排行榜小说
        /// </summary>
        /// <param name="i_book_name"></param>
        /// <param name="i_book_source"></param>
        /// <param name="i_connect_string"></param>
        /// <returns></returns>
        public long AddTopBook(string i_book_name, Consts.BookSource i_book_source)
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(m_sys_config.DBType);

            try
            {
                return t_db_mgr.AddTopBook(i_book_name, i_book_source,m_sys_config.DBLinkString);
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加热点排行榜小说失败" + ex.ToString());
                return -1;
            }
        }

          /// <summary>
        /// 添加图书点击
        /// </summary>
        /// <param name="i_book_id"></param>
        /// <param name="i_connect_string"></param>
        public void AddBookClick(long i_book_id)
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(m_sys_config.DBType);

            try
            {
                t_db_mgr.AddBookClick(i_book_id, m_sys_config.DBLinkString);
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加图书点击失败" + ex.ToString());
               
            }
        }

        /// <summary>
        /// 获取从SEO搜索的小说
        /// </summary>
        /// <returns></returns>
        public List<BookInfo> GetSEOBook()
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(m_sys_config.DBType);

            try
            {
                return t_db_mgr.GetSEOBook( m_sys_config.DBLinkString);
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取从SEO搜索的图书失败" + ex.ToString());
                return null;
            }
        }

        /// <summary>
        /// 获取从VIP搜索的小说
        /// </summary>
        /// <returns></returns>
        public List<BookInfo> GetVIPBook()
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(m_sys_config.DBType);

            try
            {
                return t_db_mgr.GetVIPBook(m_sys_config.DBLinkString);
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取从vip搜索的图书失败" + ex.ToString());
                return null;
            }
        }

          /// <summary>
        /// 获取小说的章节地址列表
        /// </summary>
        /// <param name="i_book_id"></param>
        /// <param name="i_connect_string"></param>
        /// <returns></returns>
        public List<BookUrlInfo> GetBookUrlByBookId(long i_book_id)
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(m_sys_config.DBType);

            try
            {
                List<BookUrlInfo> t_list= t_db_mgr.GetBookUrlByBookId(i_book_id, m_sys_config.DBLinkString);

                //按权重倒序
                if (t_list != null && t_list.Count > 0)
                {
                    t_list.Sort(delegate(BookUrlInfo t_a, BookUrlInfo t_b)
                    {
                        if (t_a.Bits > t_b.Bits)
                            return -1;
                        if (t_a.Bits < t_b.Bits)
                            return 1;
                        return 0;
                    });
                }

                return t_list;
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取小说的章节列表失败" + ex.ToString());
                return null;
            }
        }

        /// <summary>
        /// 获取小说信息
        /// </summary>
        /// <param name="i_book_id"></param>
        /// <param name="i_connect_string"></param>
        /// <returns></returns>
        public BookInfo GetBookInfoById(long i_book_id)
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(m_sys_config.DBType);

            try
            {
                return t_db_mgr.GetBookInfoById(i_book_id, m_sys_config.DBLinkString);
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加小说URL失败" + ex.ToString());
                return null;
            }
        }

          /// <summary>
        /// 添加小说URL
        /// </summary>
        /// <param name="i_book_url"></param>
        /// <returns></returns>
        public int AddBookUrl(BookUrlInfo i_book_url)
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(m_sys_config.DBType);

            try
            {
                return t_db_mgr.AddBookUrl(i_book_url, m_sys_config.DBLinkString);
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加小说URL失败" + ex.ToString());
                return -1;
            }
        }

        public List<BookUrlInfo> GetICONBookList()
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(m_sys_config.DBType);

            try
            {
                return t_db_mgr.GetICONBookList( m_sys_config.DBLinkString);
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取小说需要更新icon列表失败" + ex.ToString());
                return null;
            }
        }

        /// <summary>
        /// 更新小说封面地址
        /// </summary>
        /// <param name="i_book_id"></param>
        /// <param name="i_icon_url"></param>
        /// <param name="i_connect_string"></param>
        /// <returns></returns>
        public int UpdateBookUrl(long i_book_id, string i_icon_url)
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(m_sys_config.DBType);

            try
            {
                return t_db_mgr.UpdateBookUrl(i_book_id,i_icon_url, m_sys_config.DBLinkString);
            }
            catch (Exception ex)
            {
                LogHelper.Error("更新小说封面地址失败" + ex.ToString());
                return -1;
            }
        }


        public List<BookInfo> GetSEOBookChapterContent()
        {
            DataManager t_db_mgr = DBInstanceManager.CreateDbObj(m_sys_config.DBType);

            try
            {
                return t_db_mgr.GetSEOBookChapterContent(m_sys_config.DBLinkString);
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取需要更新小说章节的小说失败" + ex.ToString());
                return null;
            }
        }
    }
}
